﻿#include "YPlayVideoPreviewWindow.h"

void YPlayVideoPreviewWindow::timerEvent(QTimerEvent* ev)
{
	
	if (player.isPause())
	{
		ui.play->setStyleSheet(
			"background-image: url(:/QtForOpenCV4Tool/bin/images/icon_player_play.png);");
	}
	else
	{
		ui.play->setStyleSheet(
			"background-image: url(:/QtForOpenCV4Tool/bin/images/icon_player_pause.png);");
	}

	if (player.isPause())return;
	player.Update();
	auto pos = player.pos_ms();
	auto total = player.total_ms();
	ui.pos->setMaximum(total);
	ui.pos->setValue(pos);
	//qDebug() << "定时器正在执行";

}
void YPlayVideoPreviewWindow::Close()
{
	if (timerId != 0) {
		killTimer(timerId);
		timerId = 0; // 标记为已停止
		qDebug() << "Timer已停止";
	}
	player.Stop();
}

void YPlayVideoPreviewWindow::Pause()
{
	player.Pause(!player.isPause());

}
void YPlayVideoPreviewWindow::Move()        //进度条拖动
{
	player.Pause(true);
}
void YPlayVideoPreviewWindow::PlayPos()     //控制播放进度
{
	player.Seek(ui.pos->value());
	player.Pause(false);
}
void YPlayVideoPreviewWindow::SetSpeed()
{
	float speed = 1;
	int s = ui.speed->value();
	if (s <= 10)
	{
		speed = (float)s / (float)10;
	}
	else
	{
		speed = s - 9;
	}
	ui.speedtxt->setText(QString::number(speed));
	player.SetSpeed(speed);
}

void YPlayVideoPreviewWindow::closeEvent(QCloseEvent* ev)
{
	//Close();
}
bool YPlayVideoPreviewWindow::Open(const char* url)
{

	if (!player.Open(url, (void*)ui.video->winId()))
		return false;
	player.Start();
	player.Pause(false);//播放状态
	startTimer(10);
	return true;
}
YPlayVideoPreviewWindow::YPlayVideoPreviewWindow(QWidget* parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	timerId = startTimer(10);
}

YPlayVideoPreviewWindow::~YPlayVideoPreviewWindow()
{
	Close();
}
